package com.celesea.auth.position.service;

import com.celesea.auth.position.dao.PositionUserDao;
import com.celesea.auth.position.entity.PositionUser;
import com.celesea.auth.position.vo.PositionVo;
import com.celesea.framework.service.AbstractBaseService;
import com.celesea.framework.util.CollectionKit;
import com.celesea.framework.util.Const;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author JackYuan
 * @create 2017-11-23 20:54
 */
@Service
public class PositionUserServiceImpl extends AbstractBaseService<PositionUser> implements PositionUserService {

    @Autowired
    private PositionUserDao positionUserDao;

    @Override
    public List<PositionUser> findByPosition(String positionId) {
        return positionUserDao.selectByExample(buildExample("positionId", positionId));
    }

    @Override
    public List<PositionUser> findByPosition(String positionId, String deptId) {
        Example example = new Example(PositionUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("positionId", positionId);
        criteria.andEqualTo("deptId", deptId);
        return positionUserDao.selectByExample(example);
    }

    @Override
    public List<PositionUser> findByPosition(String positionId, List<String> deptIds) {
        Example example = new Example(PositionUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("positionId", positionId);
        criteria.andEqualTo("extend", Const.Bool.TRUE);
        criteria.andIn("deptId", deptIds);
        return positionUserDao.selectByExample(example);
    }

    @Override
    public int deleteByPosition(PositionVo positionVo) {
        //只删除当前部门的授权
        Example example = new Example(PositionUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("positionId", positionVo.getId());
        if (positionVo.getCurrentDept() != null) {
            criteria.andEqualTo("deptId", positionVo.getCurrentDept().getId());
        }
        return positionUserDao.deleteByExample(example);
    }


    @Override
    public int insertByPosition(PositionVo entity) {
        List<PositionUser> positionUsers = Lists.newArrayList();
        if (CollectionKit.isNotEmpty(entity.getUsers())) {
            //只新增当前部门的授权
            entity.getUsers().stream().forEach(item -> positionUsers.add(generatPk(item.toEntity())));
        }
        if (CollectionKit.isNotEmpty(positionUsers)) {
            return positionUserDao.insertList(positionUsers);
        }
        return 0;
    }

    @Override
    public int deleteByPosition(String positionId, String deptId) {
        Example example = new Example(PositionUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("positionId", positionId);
        criteria.andEqualTo("deptId", deptId);
        return positionUserDao.deleteByExample(example);
    }

    @Override
    public List<PositionUser> findByUser(String userId) {
        return positionUserDao.selectByExample(buildExample("userId", userId));
    }

    private Example buildExample(String key, String positionId) {
        Example example = new Example(PositionUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(key, positionId);
        return example;
    }
}
